package com.lijie.bs_recruit.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.lijie.bs_recruit.VO.ResultVO;
import com.lijie.bs_recruit.model.Applicants;
import com.lijie.bs_recruit.model.Hr;
import com.lijie.bs_recruit.model.Position;
import com.lijie.bs_recruit.service.ApplicantsService;
import com.lijie.bs_recruit.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lijie
 * @since 2021-04-07
 */
@RestController
@RequestMapping("/applicants")
public class ApplicantsController {
    @Autowired
    ApplicantsService applicantsService;

    @Autowired
    PositionService positionService;

    //增加请求记录
    @RequestMapping("/AddApplicant")
    public String AddApplication(@RequestBody Applicants applicants){
        String result="isExist";
        Applicants applicants1 = applicantsService.seApplicantByIds(applicants.getUserId(), applicants.getPositionId());
        if(applicants1==null){
            boolean save = applicantsService.save(applicants);
            if(save){
                result="succ";
            }else{
                result="error";
            }
        }
        return result;
    }

    //用户查看自己全部的投递记录
    @RequestMapping("/selApplicantByuserId/{pageIndex}")
    public PageInfo<Applicants> selApplicantByuserId(@PathVariable Integer pageIndex ,@RequestBody Applicants applicants){
        PageInfo<Applicants> applicantsPageInfo=new PageInfo<>();
        if(applicants.getUserId()!=null){
            applicantsPageInfo= applicantsService.selApplicantByuserId(pageIndex, applicants.getUserId());
        }
        return applicantsPageInfo;
    }

    //用户查看通过初筛的投递记录
    @RequestMapping("/selApplicantByOne/{pageIndex}")
    public PageInfo<Applicants> selApplicantByone(@PathVariable Integer pageIndex ,@RequestBody Applicants applicants){
        PageInfo<Applicants> applicantsPageInfo=new PageInfo<>();
        if(applicants.getUserId()!=null){
            applicantsPageInfo= applicantsService.selApplicantByUserIndex(pageIndex, applicants.getUserId(),1);
        }
        return applicantsPageInfo;
    }

    //用户查看通过需要面试的职位
    @RequestMapping("/selApplicantByTwo/{pageIndex}")
    public PageInfo<Applicants> selApplicantBytwo(@PathVariable Integer pageIndex ,@RequestBody Applicants applicants){
        PageInfo<Applicants> applicantsPageInfo=new PageInfo<>();
        if(applicants.getUserId()!=null){
            applicantsPageInfo= applicantsService.selApplicantByUserIndex(pageIndex, applicants.getUserId(),2);
        }
        return applicantsPageInfo;
    }

    //用户查看已经不合适的职位
    @RequestMapping("/selApplicantByThree/{pageIndex}")
    public PageInfo<Applicants> selApplicantByThree(@PathVariable Integer pageIndex ,@RequestBody Applicants applicants){
        PageInfo<Applicants> applicantsPageInfo=new PageInfo<>();
        if(applicants.getUserId()!=null){
            applicantsPageInfo= applicantsService.selApplicantByUserIndex(pageIndex, applicants.getUserId(),3);
        }
        return applicantsPageInfo;
    }

    //用户查看已经面试通过的职位
    @RequestMapping("/selApplicantByFour/{pageIndex}")
    public PageInfo<Applicants> selApplicantByFour(@PathVariable Integer pageIndex ,@RequestBody Applicants applicants){
        PageInfo<Applicants> applicantsPageInfo=new PageInfo<>();
        if(applicants.getUserId()!=null){
            applicantsPageInfo= applicantsService.selApplicantByUserIndex(pageIndex, applicants.getUserId(),4);
        }
        return applicantsPageInfo;
    }

    //hr查看自己收到的简历
    @RequestMapping("/selApplicantByHr/{pageIndex}")
    public PageInfo<Applicants> selApplicantByHr(@PathVariable Integer pageIndex ,@RequestBody Hr hr){
        PageInfo<Applicants> applicantsPageInfo=new PageInfo<>();
        if(hr.getHrId()!=null){
            applicantsPageInfo= applicantsService.selApplicantByHrIndex(pageIndex, hr.getHrId(),0);
        }
        return applicantsPageInfo;
    }

    //hr查看自己待定简历
    @RequestMapping("/selApplicantByHrOne/{pageIndex}")
    public PageInfo<Applicants> selApplicantByHrOne(@PathVariable Integer pageIndex ,@RequestBody Hr hr){
        PageInfo<Applicants> applicantsPageInfo=new PageInfo<>();
        if(hr.getHrId()!=null){
            applicantsPageInfo= applicantsService.selApplicantByHrIndex(pageIndex, hr.getHrId(),1);
        }
        return applicantsPageInfo;
    }
    //hr查看自己面试简历
    @RequestMapping("/selApplicantByHrTwo/{pageIndex}")
    public PageInfo<Applicants> selApplicantByHrTwo(@PathVariable Integer pageIndex ,@RequestBody Hr hr){
        PageInfo<Applicants> applicantsPageInfo=new PageInfo<>();
        if(hr.getHrId()!=null){
            applicantsPageInfo= applicantsService.selApplicantByHrIndex(pageIndex, hr.getHrId(),2);
        }
        return applicantsPageInfo;
    }
    //hr查看自己不合适简历
    @RequestMapping("/selApplicantByHrThree/{pageIndex}")
    public PageInfo<Applicants> selApplicantByHrThree(@PathVariable Integer pageIndex ,@RequestBody Hr hr){
        PageInfo<Applicants> applicantsPageInfo=new PageInfo<>();
        if(hr.getHrId()!=null){
            applicantsPageInfo= applicantsService.selApplicantByHrIndex(pageIndex, hr.getHrId(),3);
        }
        return applicantsPageInfo;
    }

    //hr查看自己面试通过的
    @RequestMapping("/selApplicantByHrFour/{pageIndex}")
    public PageInfo<Applicants> selApplicantByHrFour(@PathVariable Integer pageIndex ,@RequestBody Hr hr){
        PageInfo<Applicants> applicantsPageInfo=new PageInfo<>();
        if(hr.getHrId()!=null){
            applicantsPageInfo= applicantsService.selApplicantByHrIndex(pageIndex, hr.getHrId(),4);
        }
        return applicantsPageInfo;
    }

    //hr设置简历为待处理
    @RequestMapping("/setApplicantIsEmployOne")
    public ResultVO setApplicantIsEmploy(@RequestBody Applicants applicants){
        ResultVO resultVO=new ResultVO();
        applicants.setIsEmploy(1);
        boolean b = applicantsService.setApplicantIsEmploy(applicants);
        if(b){
            resultVO.setMessage("succ");
        }else{
            resultVO.setMessage("error");
        }
        return resultVO;
    }
    //hr设置简历为通知面试
    @RequestMapping("/setApplicantIsEmployTwo")
    public ResultVO setApplicantIsEmployTwo(@RequestBody Applicants applicants){
        ResultVO resultVO=new ResultVO();
        applicants.setIsEmploy(2);
        boolean b = applicantsService.setApplicantIsEmploy(applicants);
        if(b){
            resultVO.setMessage("succ");
        }else{
            resultVO.setMessage("error");
        }
        return resultVO;
    }
    //hr设置简历为不合适
    @RequestMapping("/setApplicantIsEmployThree")
    public ResultVO setApplicantIsEmployThree(@RequestBody Applicants applicants){
        ResultVO resultVO=new ResultVO();
        applicants.setIsEmploy(3);
        boolean b = applicantsService.setApplicantIsEmploy(applicants);
        if(b){
            resultVO.setMessage("succ");
        }else{
            resultVO.setMessage("error");
        }
        return resultVO;
    }

    //hr设置简历面试通过 ， 需求人数减一
    @RequestMapping("/setApplicantIsEmployFour")
    public ResultVO setApplicantIsEmployFour(@RequestBody Applicants applicants){
        ResultVO resultVO=new ResultVO();
        Position positionById = positionService.getById(applicants.getPositionId());
        if(positionById.getNeedNums()>0){
            applicants.setIsEmploy(4);
            boolean b = applicantsService.setApplicantIsEmploy(applicants);
            if(b){
                UpdateWrapper<Position> wrapper=new UpdateWrapper<>();
                wrapper.eq("positionId", positionById.getPositionId())
                        .set("needNums",positionById.getNeedNums()-1);
                positionService.update(positionById,wrapper );
                resultVO.setMessage("succ");
            }else{
                resultVO.setMessage("error");
            }
        }

        return resultVO;
    }

}

